$(document).ready(function(){
	
	$("#punicao").val(punicao);
	//datepickers
	datePicker("dataRegistro");

	//carrega os combo box
	loadToComboBoxUrl("#justificacao","justificacoeses","justificativa");

	//circunstancias
	var id = "justificativasDataGrid";
	var justificativasAoColumns = [
                            	{"sTitle": "Id", "mDataProp": "justificacao.id", "bVisible": false, "sDefaultContent":""},
                            	{"sTitle": "Justificativa", "mDataProp": "justificacao.justificativa", "bVisible": true, "sDefaultContent":""}
//                            	{"sTitle": "Opções", "mDataProp": "htmlControl","bSortable": false, "bVisible": true, "sWidth": "45px","sDefaultContent":"", 
//                 				   "fnRender": function ( oObj ) {
//                 						
//                 					   var botao = "";	                     					   
////                 					   if(oObj.aData.despachar){
//                 						   botao = "<button type=\"button\" class=\"editarBtn"+id+"\" onclick=\"populaElogio(event,'"+id+"')\">Editar Elogio</button>";
////                 					   }
//                 					   //botao+= "<button type=\"button\" class=\"magnifierGlassDataGridBtn"+id+"\" onclick=\"loadDespacho(event, '"+oObj.aData.id+"')\">Vizualizar despachos</button>";
//                 					   botao+= "<button type=\"button\" class=\"printBtn"+id+"\" onclick=\"imprimir(event, '"+oObj.aData.id+"')\">Imprimir Elogio</button>";
//                 					   return  botao;
//	                 				}
//	                 			}
							];
	//oneToMany(id, JSON.stringify(elogiosAoColumns));
 	oneToMany(id, JSON.stringify(justificativasAoColumns, function(key, val) {if (typeof val === 'function') { return val + '';} return val;}));
	
	$("#incluirJustificativaBtn").button({icons: {primary: "ui-icon-plus"}, text: false}).click(
			function(){		
				registro = $("#"+$("#cadastro_justificativa").attr("id")).formParams(false);			
				salvarReturn("cadastro_justificativa", "justificacoespunicoeses", registro, loadJustificativas);
	});	
	
	loadJustificativas();
    
});	

function loadJustificativas(){
	$("#justificativasDataGrid").dataTable().fnClearTable();
	$.ajax({
		"type" : "GET",
		"contentType" : "application/json",
		"dataType" : "json",
		"url" : "justificacoespunicoeses?find=ByPunicao&punicao="+punicao,
		"error" : trataErro,
		"success" : function(result) {			
			$("#justificativasDataGrid").dataTable().fnAddData(result);
		}
	});
}
